package sculove.spring.framework.common;

import java.lang.reflect.Field;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.Validator;

public abstract class AbstractDto implements Validator {
    @SuppressWarnings("unused")
    protected final Log logger = LogFactory.getLog(getClass());
    
    public String toString() {
        StringBuffer tempBuffer = new StringBuffer();
        tempBuffer.append("\n [" + this.getClass().getName() + "]");

        Field[] myFieldArray = this.getClass().getDeclaredFields();

        for (int i = 0; i < myFieldArray.length; i++) {
            try {
                Field myField = myFieldArray[i];
                myField.setAccessible(true);

                if (myField.getType().isArray()) {
                    String className = myField.getType().getName();
                    className = className.substring(2, className.length() - 1);

                    tempBuffer.append("\n " + className + "[] ").append(myField.getName()).append(
                            " = " + Arrays.asList((Object[]) myField.get(this)));
                } else if (myField.getType().isPrimitive() || (myField.getType() == String.class)) {
                    String typeName = myField.getType().getName();
                    typeName = ((myField.getType() == String.class) ? "String" : typeName);
                    tempBuffer.append("\n " + typeName + " ").append(myField.getName()).append(" = [" + myField.get(this) + "]");
                } else if (myField.getType() == Class.class) {
                    // ignore
                } else {
                    String className = myField.getType().getName();
                    tempBuffer.append("\n " + className + " ").append(myField.getName()).append(" = [" + myField.get(this) + "]");
                }
            } catch (Exception e) {
            }
        }
        return tempBuffer.toString();
    }    
    
	@Override
	public boolean supports(Class clazz) {
		return this.getClass().isAssignableFrom(clazz);
	}    
}
